package cn.edu.scau.cmi.liyetian.client.spring;

import java.util.List;
import java.util.Scanner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.liyetian.client.hibernate.AdminClient;
import cn.edu.scau.cmi.liyetian.dao.StudentDao;
import cn.edu.scau.cmi.liyetian.dao.StudentDaoImpl;
import cn.edu.scau.cmi.liyetian.dao.TeacherDao;
import cn.edu.scau.cmi.liyetian.dao.TeacherDaoImpl;
import cn.edu.scau.cmi.liyetian.domain.Student;
import cn.edu.scau.cmi.liyetian.domain.Teacher;

public class Spring02Client {
	public static ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
	public static void OpForStudent() {
		System.out.println("请输入对学生进行的操作，add,update,delete,tutorChoose,tutorUpdate");
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		if(str.equals("add")) {
			System.out.println("输入id");
			int stuId = Integer.parseInt(scan.next());
			System.out.println("输入名字");
			String name = scan.next();
			
			Student student = new Student();
			student.setId(stuId);
			student.setName(name);
			
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			studentDao.addStudent(student);
			System.out.println("学生"+student+"已经添加");
		}
		else if(str.equals("update")) {
			System.out.println("输入id");
			int stuId = Integer.parseInt(scan.next());
			System.out.println("输入名字");
			String name = scan.next();
			
			Student student = new Student();
			student.setId(stuId);
			student.setName(name);
			
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			studentDao.updateStudent(student);
			System.out.println("学生"+student+"已经更新");
		}
		else if(str.equals("delete")) {
			System.out.println("输入id");
			int id = Integer.parseInt(scan.next());
			Student student = new Student();
			student.setId(id);
			
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			studentDao.deleteStudent(student);
			System.out.println("学生"+student.getId()+"已经删除");
		}
		else if(str.equals("tutorChoose")) {
			System.out.println("输入学生id");
			int stuid = Integer.parseInt(scan.next());
			System.out.println("输入选择的导师id");
			int tutorid = Integer.parseInt(scan.next());
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			Student student = studentDao.getStudent(stuid);
			student.setTutor(tutorid);
			studentDao.updateStudent(student);
			System.out.println("学生指导老师已经选择");
		}
		else if(str.equals("tutorUpdate")) {
			System.out.println("输入学生id");
			int stuid = Integer.parseInt(scan.next());
			System.out.println("输入选择的导师id");
			int tutorid = Integer.parseInt(scan.next());
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			Student student = studentDao.getStudent(stuid);
			student.setTutor(tutorid);
			studentDao.updateStudent(student);
			System.out.println("学生指导老师已经修改");
		}
	}
	public static void OpForTeacher() {
		System.out.println("请输入对老师进行的操作，add,update,delete,tutorChoose,tutorUpdate");
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		if(str.equals("add")) {
			System.out.println("输入id");
			int teaId = Integer.parseInt(scan.next());
			System.out.println("输入名字");
			String name = scan.next();
			
			Teacher teacher = new Teacher();
			teacher.setId(teaId);
			teacher.setName(name);
			
			TeacherDao teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
			teacherDao.addTeacher(teacher);
			System.out.println("老师"+teacher+"已经添加");
		}
		else if(str.equals("update")) {
			System.out.println("输入id");
			int teaId = Integer.parseInt(scan.next());
			System.out.println("输入名字");
			String name = scan.next();
			
			Teacher teacher = new Teacher();
			teacher.setId(teaId);
			teacher.setName(name);
			
			TeacherDao teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
			teacherDao.updateTeacher(teacher);
			System.out.println("老师"+teacher+"已经更新");
		}
		else if(str.equals("delete")) {
			System.out.println("输入id");
			int id = Integer.parseInt(scan.next());
			Teacher teacher = new Teacher();
			teacher.setId(id);
			
			TeacherDao teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
			teacherDao.deleteTeacher(teacher);
			System.out.println("老师"+teacher.getId()+"已经删除");
		}
		else if(str.equals("tutorChoose")) {
			//
			System.out.println("输入被指导学生id");
			int id = Integer.parseInt(scan.next());
			System.out.println("输入指导老师id");
			int id2 = Integer.parseInt(scan.next());
			
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			Student student = studentDao.getStudent(id);
			
			student.setTutor(id2);
			
			studentDao.updateStudent(student);
			System.out.println("被指导学生"+student+"已更新");
			
			
		}
		else if(str.equals("tutorUpdate")) {
			//
			System.out.println("输入老师id");
			int id = Integer.parseInt(scan.next());
			System.out.println("输入修改后老师id");
			int id2 = Integer.parseInt(scan.next());
			
			TeacherDao teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
			List<Student> list = teacherDao.getTutor(id);
			StudentDao studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
			
			for (Student student : list) {
				if(student.getTutor()==id) {
					System.out.println(student);
				}
	        }
			
			for (Student student : list) {
				if(student.getTutor()==id) {
					student.setTutor(id2);
					studentDao.updateStudent(student);
					System.out.println(student+"更新成功");
				}        
	        }
		}
	}
	public static void main(String[] args) {
		System.out.println("请输入操作的对象，student,teacher");
		Scanner scan = new Scanner(System.in);
		String str = scan.next();
		if(str.equals("student")) {
			AdminClient.OpForStudent();
		}
		else if(str.equals("teacher")) {
			AdminClient.OpForTeacher();
		}
	}
}
